﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace _0912089
{
    public class Button : SingleView
    {
        private Texture2D _hoverTexture;
        private bool _isHover = false;
        private bool _isHaveHoverTexture = true;

        public bool IsHover
        {
            get { return _isHover; }
            set { _isHover = value; }
        }

        public Button(string strTexture, Vector2 pos, float scale)
        {
            _background = GameBase.Constant.content.Load<Texture2D>(strTexture);

            try
            {
                _hoverTexture = GameBase.Constant.content.Load<Texture2D>(strTexture + "_hover");
            }
            catch
            {
                _isHaveHoverTexture = false;
                _hoverTexture = _background;
            }

            Position = pos;
            _scale = scale;
            _size = new Vector2(_background.Width, _background.Height);            
        }

        public override void draw(GameTime gameTime, SpriteBatch spriteBatch, Color color)
        {
            if (_isHover)
                spriteBatch.Draw(_hoverTexture, Position, null, color, 0, Vector2.Zero, Scale, SpriteEffects.None, 0);
            else
            {
                if(_isHaveHoverTexture)
                    spriteBatch.Draw(_background, Position, null, color, 0, Vector2.Zero, Scale, SpriteEffects.None, 0);
                else
                    spriteBatch.Draw(_background, Position, null, color * 0.5f, 0, Vector2.Zero, Scale, SpriteEffects.None, 0);
            }
        }

        public bool isContainPoint(Vector2 point)
        {
            if (point.X >= Position.X && point.X <= Position.X + Size.X * Scale &&
                point.Y >= Position.Y && point.Y <= Position.Y + Size.Y * Scale)
                return true;
            return false;
        }

        public override void update(GameTime gameTime)
        {
            base.update(gameTime);

            MouseState ms = Mouse.GetState();
            if (isContainPoint(new Vector2(ms.X, ms.Y)))
                _isHover = true;
            else
                _isHover = false;
        }
    }
}
